//
//  MqMyViewController.m
//  Meiqi
//
//  Created by TwistedFate on 16/5/20.
//  Copyright © 2016年 KFC. All rights reserved.
//

#import "MqMyViewController.h"
#import "MqRegisterViewController.h"
#import "MqMineMainTableViewCell.h"
#import "MqMineCollectViewController.h"
#import "MqForumPostViewController.h"
#import <SDImageCache.h>
@interface MqMyViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) NSMutableArray *imgsArray;

@property (nonatomic, strong) NSMutableArray *titlesArray;
@end

@implementation MqMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    
    _imgsArray = [NSMutableArray array];
    
    for (int i = 1; i < 6; i++) {
        
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"mine%d",i]];
        
        [_imgsArray addObject:image];
        
    }
    
    
    _imageButton = [UIButton buttonWithType:(UIButtonTypeCustom)];
    
    [_imageButton setBackgroundImage:[UIImage imageNamed:@"icon_avatar@2x"] forState:(UIControlStateNormal)];
    _imageButton.frame = CGRectMake(160, 50, 60, 60);
    _imageButton.layer.cornerRadius = 30;
    _imageButton.layer.masksToBounds = YES;
    _imageButton.backgroundColor = [UIColor redColor];
    [self.view addSubview:_imageButton];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.frame = CGRectMake(_imageButton.left - 20, _imageButton.bottom, _imageButton.width + 40, 40);
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"account"];
    _nameLabel.text = name;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_nameLabel];
    
    
    _titlesArray = [NSMutableArray arrayWithObjects:@"草稿箱", @"收藏", @"当前版本号", @"清除缓存", @"退出当前账户",
                    nil];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 180, kScreenWidth, kScreenHeight - 100) style:(UITableViewStylePlain)];
    _tableView.dataSource = self;
    
    _tableView.delegate = self;
    
    _tableView.rowHeight = 60;
    
    [self.view addSubview:_tableView];
    //  注册cell
    [_tableView registerClass:[MqMineMainTableViewCell class] forCellReuseIdentifier:@"MqMineMainTableViewCell"];
    
}


#pragma mark --- 代理方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return _imgsArray.count;
    
}

//  返回cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MqMineMainTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MqMineMainTableViewCell" forIndexPath:indexPath];
    
    cell.imageV.image = _imgsArray[indexPath.row];
    cell.titleLabel.text = _titlesArray[indexPath.row];
    cell.iconImageV.image = [UIImage imageNamed:@"icon_mine_arrow_right"];
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    switch (indexPath.row) {
            //  草稿箱
        case 0:
        {
            MqForumPostViewController *postVC = [[MqForumPostViewController alloc] init];
            
            [self.navigationController pushViewController:postVC animated:YES];
            
        }
            break;
          
            //  收藏
        case 1:
        {
            MqMineCollectViewController *collectVC = [[MqMineCollectViewController alloc] init];
            
            [self.navigationController pushViewController:collectVC animated:YES];
        }
            break;
            
            //  当前版本号
        case 2:
        {
           
            
        }
            break;
            
            //  清楚缓存
        case 3:
        {
            
            float tmpSize = [[SDImageCache sharedImageCache] getSize];
            
            NSString *clearString = [NSString stringWithFormat:@"清楚缓存%.1fM",tmpSize / 1024 / 1024];
            
            [[SDImageCache sharedImageCache] clearDisk];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:clearString delegate:self cancelButtonTitle:nil otherButtonTitles:nil];
            [alert show];
            
            
            [self performSelector:@selector(finishAlert:) withObject:alert afterDelay:1];
    
        }
            break;
        
        case 4:
        {
            
            [self.view.superview sendSubviewToBack:self.view];
            
            //  没有记住密码
            if(![[NSUserDefaults standardUserDefaults] boolForKey:@"rmbpwd"])
                [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"pwd"];
            
                [[NSUserDefaults standardUserDefaults] setObject:nil forKey:@"account"];
            
        }
            break;
            
        default:
            break;
    }
    
    
    
}

- (void)finishAlert:(UIAlertView *)alert{
    
    [alert dismissWithClickedButtonIndex:0 animated:YES];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
